package com.iraytek.rtspdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.graphics.ImageFormat;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Environment;
import android.util.Size;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.iraytek.gles.render.YuvRender;
import com.iraytek.rtspdemo.util.PermissionHelper;
import com.iraytek.rtsplib.ServerEncodeManager;

import java.io.File;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private GLSurfaceView surfaceView;
    private YuvRender render;
    private RtspEncodeManager encodeManager;
    private ServerEncodeManager encodeManager1;
    private TextView textView;
    private ImageAvailableListener availableListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv);
        surfaceView = findViewById(R.id.sv);
        surfaceView.setEGLContextClientVersion(2);
        render = new YuvRender(surfaceView);
        surfaceView.setRenderer(render);
        surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        encodeManager = new RtspEncodeManager(this, 1935, new DefaultConnectionCheckRtsp());
//        encodeManager1 = new RtspEncodeManager(this, 2035, new DefaultConnectionCheckRtsp());
        String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        PermissionHelper.checkPermissions(this, permissions, this::tryOpenCamera);
    }

    private void tryOpenCamera() {
        if (render.prepared()) {
            availableListener = new ImageAvailableListener(render);
            Size size = CameraUtil.openCamera(0, ImageFormat.YUV_420_888, availableListener, null, 1600);
            encodeManager.prepareVideo(size.getWidth(), size.getHeight(), 4);
//            encodeManager.prepareAudio();
            availableListener.setImageCallback(encodeManager);
        } else {
            textView.postDelayed(this::tryOpenCamera, 50);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        CameraUtil.closeAllCameras();
        encodeManager.stopStream();
        encodeManager.stopRecording();
        availableListener.quit();
    }

    public void startServer(View view) {
        Button button = (Button) view;
        if (encodeManager.isStreaming()) {
            encodeManager.stopStream();
            textView.setText("");
            button.setText("start server");
        } else {
            textView.setText(encodeManager.getRtspAddress());
            button.setText("stop server");
            if (encodeManager != null) {
                encodeManager.startStream();
            }
        }
    }

    public void record(View view) {
        if (view.isSelected()) {
            encodeManager.stopRecording();
            view.setSelected(false);
        } else {
            String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath();
            encodeManager.startRecording(dir + File.separator + System.currentTimeMillis() + "r.mp4");
            view.setSelected(true);
        }
    }
}